package com.hutu.comment.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hutu.comment.adaptor.ResultDtoAdaptor;
import com.hutu.comment.common.QuestionStatus;
import com.hutu.comment.domain.TbComment;
import com.hutu.comment.domain.TbEditorData;
import com.hutu.comment.domain.TbQuestion;
import com.hutu.comment.domain.TbUserCollectedQuestion;
import com.hutu.comment.domain.TbUserLikedQuestion;
import com.hutu.comment.domain.TbUserVisitedQuestion;
import com.hutu.comment.domain.dto.ResultDto;
import com.hutu.comment.service.TbEditorDataService;
import com.hutu.comment.service.TbQuestionService;
import com.hutu.comment.mapper.TbQuestionMapper;
import com.hutu.comment.service.TbUserCollectedQuestionService;
import com.hutu.comment.service.TbUserLikedQuestionService;
import com.hutu.comment.service.TbUserVisitedQuestionService;
import com.hutu.comment.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 20272
* @description 针对表【tb_question(问题表)】的数据库操作Service实现
* @createDate 2025-03-17 22:08:45
*/
@Service
@RequiredArgsConstructor
public class TbQuestionServiceImpl extends ServiceImpl<TbQuestionMapper, TbQuestion>
    implements TbQuestionService{
    private final TbEditorDataService tbEditorDataService;
    private final TbUserVisitedQuestionService tbUserVisitedQuestionService;
    private final TbUserLikedQuestionService tbUserLikedQuestionService;
    private final TbUserCollectedQuestionService tbUserCollectedQuestionService;
    private final TbCommentServiceImpl tableCommentService;
    @Override
    public ResultDto findContentByTitle(String title) {
        // 根据标题查询问题 只查询所需要的字段,提高性能
        TbQuestion tbQuestion = lambdaQuery()
                .select(TbQuestion::getId, TbQuestion::getTitle
                        , TbQuestion::getAnswer, TbQuestion::getAnswerer
                        , TbQuestion::getQuestion)
                .like(TbQuestion::getTitle, title)
                .eq(TbQuestion::getStatus, QuestionStatus.UP)
                .eq(TbQuestion::getDeleteFlag, QuestionStatus.NOT_DELETE)
                .orderByDesc(TbQuestion::getCreateTime)
                .one();

        // 字符串处理转成tag集合
        String tag = tbQuestion.getTags();
        List<String> tags = StringUtils.split(tag, ",");
        // 获取浏览数量
        String tbQuestionId = tbQuestion.getId();
        Long count = tbUserVisitedQuestionService.getBaseMapper()
                .selectCount(
                        Wrappers.lambdaQuery(TbUserVisitedQuestion.class)
                                .eq(TbUserVisitedQuestion::getQuestionId, tbQuestionId)
                );
        // 是否喜欢
        boolean liked = false;
        String answerer = tbQuestion.getAnswerer();
        TbUserLikedQuestion tbUserLikedQuestion = new TbUserLikedQuestion();
        tbUserLikedQuestion.setQuestionId(tbQuestionId);
        tbUserLikedQuestion.setUsername(answerer);
        TbUserLikedQuestion likedQuestion = tbUserLikedQuestionService.selectByMultiId(tbUserLikedQuestion);
        if (likedQuestion != null){
            liked = true;
        }

        // 是否收藏
        boolean collected = false;
        TbUserCollectedQuestion tbUserCollectedQuestion = new TbUserCollectedQuestion();
        tbUserCollectedQuestion.setQuestionId(tbQuestionId);
        tbUserCollectedQuestion.setUsername(answerer);
        TbUserCollectedQuestion collectedQuestion = tbUserCollectedQuestionService.selectByMultiId(tbUserCollectedQuestion);
        if (collectedQuestion != null){
            collected = true;
        }
        // 获取评论数
        Long commentCount = tableCommentService.lambdaQuery()
                .eq(TbComment::getQuestionId, tbQuestionId)
                .count();
        // 获取答案信息
        Integer question = tbQuestion.getQuestion();
        TbEditorData tbEditorData = tbEditorDataService.getBaseMapper().selectById(question);
        String html = tbEditorData.getHtml();
        // 组装结果
        ResultDto resultDto = ResultDtoAdaptor.adapt(tags, count, liked,collected, commentCount, html);
        return resultDto;
    }
}




